import * as gulp from 'gulp';
import * as gulpLoadPlugins from 'gulp-load-plugins';
import { join } from 'path';

import Config from '../../config';
import { makeTsProject, TemplateLocalsBuilder } from '../../utils';

const plugins = <any>gulpLoadPlugins();

const INLINE_OPTIONS = {
    base: Config.TMP_DIR,
    target: 'es5',
    useRelativePaths: true,
    removeLineBreaks: true
};

/**
 * Executes the build process, transpiling the TypeScript files for the production environment.
 */

export = () => {
    let tsProject = makeTsProject({}, Config.TMP_DIR);
    let src = [
        Config.TOOLS_DIR + '/manual_typings/**/*.d.ts',
        join(Config.TMP_DIR, '**/*.ts')
    ];
    let result = gulp.src(src)
        .pipe(plugins.plumber())
        .pipe(plugins.inlineNg2Template(INLINE_OPTIONS))
        .pipe(tsProject())
        .once('error', function(e: any) {
            this.once('finish', () => process.exit(1));
        });

    gulp.src([
        join(Config.PUBLIC_SRC_JS, '*.js'),
        join(Config.PUBLIC_SRC_JS, '**/*.js'),
    ])
        .pipe(plugins.uglify())
        .pipe(gulp.dest(Config.JS_DEST));

    return result.js
        .pipe(plugins.template(new TemplateLocalsBuilder().build()))
        .pipe(gulp.dest(Config.TMP_DIR))
        .on('error', (e: any) => {
            console.log(e);
        });
};
